package com.applink.sell_proof.builder;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;

import com.applink.sell_proof.R;
import com.applink.sell_proof.SearchProductModel;
import com.applink.sell_proof.model.Product;
import com.applink.sell_proof.util.Utility;

public class ProofBuilderExt extends ProofBuilder {

	private List<Product> mRemovedList = new ArrayList<Product>();

	private boolean noedit = false;

	public ProofBuilderExt(Context context, ViewGroup parent) {
		super(context, parent);
	}

	@Override
	public View buildConvertView() {

		if (!noedit)
			return super.buildConvertView();

		View layout = mLayoutInflater.inflate(R.layout.proof_item, null);
		Utility.disableTextView(InputType.TYPE_NULL, (ViewGroup) layout);

		return layout;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.model_no:
			mSelectedPosition = (Integer) v.getTag();
			Intent intent = new Intent(mContext, SearchProductModel.class);
			((Activity) mContext).startActivityForResult(intent,
					SearchProductModel.SEARCH_PRODUCT);
			break;
		case R.id.pdremove:
			int position = (Integer) v.getTag();

			Product p = (Product) mData.get(position);

			if ((p.getProductOpportunityId() != 0)
					|| (p.getProductClaimId() != 0)) {
				p.setDelete(true);
				mRemovedList.add(p);
			}

			mData.remove(position);
			notifyDataChange();
			break;
		default:
			break;
		}
	}

	public List<Product> getRemovedProductList() {
		return mRemovedList;
	}

	public void setNoedit(boolean n) {
		noedit = n;
	}

	public void setData(List<? extends Object> data) {

		List<Product> sdata = new ArrayList<Product>(), rdata = new ArrayList<Product>();

		for (Object p : data)
			if (((Product) p).isDelete())
				rdata.add((Product) p);
			else
				sdata.add((Product) p);

		mRemovedList = rdata;

		super.setData(sdata);
	}
}